package com.xs.ahoi.hcxs.ui.dialog;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.xs.ahoi.hcxs.R;

/**
 * 通用的确认删除弹窗页面
 */
public class DialogCommonDelete extends BaseDialog {

    @BindView(R.id.deleteLocal)
    public LinearLayout deleteLocal;
    @BindView(R.id.ivDeleteLocal)
    public ImageView ivDeleteLocal;
    @BindView(R.id.tvDeleteLocal)
    public TextView tvDeleteLocal;

    // 是否删除本地
    private boolean isDeleteLocal = false;
    // 选中和未选中时图标
    private Drawable drawableUncheck, drawableChecked;
    private DeleteListener deleteListener;

    public DialogCommonDelete(Context context) {
        super(context, R.style.common_dialog);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_common_delete;
    }

    @Override
    protected int showPosition() {
        return Gravity.CENTER;
    }

    @Override
    protected void initData() {
        // 右上角删除按钮复选框
        drawableUncheck = ContextCompat.getDrawable(getContext(), R.drawable.icon_rect_uncheck);
        drawableChecked = ContextCompat.getDrawable(getContext(), R.drawable.icon_rect_check);
    }

    // 显示复选框
    public void showCheckbox() {
        deleteLocal.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.tvDialogOk, R.id.tvDialogCancel, R.id.deleteLocal})
    public void onClick(View v) {
        switch (v.getId()) {
            // 确认
            case R.id.tvDialogOk:
                deleteListener.delete(isDeleteLocal);
                dismiss();
                break;
            // 取消
            case R.id.tvDialogCancel:
                dismiss();
                break;
            // 是否删除本地
            case R.id.deleteLocal:
                if (isDeleteLocal) {
                    ivDeleteLocal.setImageDrawable(drawableUncheck);
                    tvDeleteLocal.setTextColor(ContextCompat.getColor(getContext(), R.color.colorBlackLittle));
                } else {
                    ivDeleteLocal.setImageDrawable(drawableChecked);
                    tvDeleteLocal.setTextColor(ContextCompat.getColor(getContext(), R.color.colorRed));
                }
                isDeleteLocal = !isDeleteLocal;
                break;
        }
    }

    public void setDeleteListener(DeleteListener deleteListener) {
        this.deleteListener = deleteListener;
    }

    public interface DeleteListener {
        void delete(boolean isDeleteLocal);
    }
}
